Preserve select_related joins in values()/values_list() (#2004)#2224
Open
gaoflow wants to merge 1 commit into
Open
Preserve select_related joins in values()/values_list() (#2004)#2224gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
QuerySet.values() and values_list() dropped the LEFT OUTER JOINs that select_related() had requested. When an annotation, filter or ordering referenced the related table (e.g. a RawSQL term), the generated SQL referenced an alias with no matching join and raised "no such column". Thread the requested select_related lookups through to the value queries and register the joins (without selecting the related columns, which the projection does not need) so the alias resolves, matching QuerySet. Fixes tortoise#2004.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #2004.
select_related(...)requests aLEFT OUTER JOIN, but.values()and.values_list()silently dropped it. When an annotation, filter or ordering referenced the joined table — e.g. aRawSQLterm used for something the ORM can't express directly, like a cast — the generated SQL referenced the related alias with no matching join, so the query crashed:The regular
QuerySetpath joins correctly (QuerySet._make_querycalls_join_select_related); only the value-query path was missing it.Fix
Thread the requested
select_relatedlookups through toValuesQuery/ValuesListQueryand register the joins in their_make_query. A smallAwaitableQuery._join_select_related_tables()helper adds only the joins (not the related columns, which the projection does not select), so:.values()/.values_list()output shape is unchanged (no extra columns);select_relatedare unaffected (no spurious joins).Tests
Added
test_select_related_join_preserved_in_valuesand..._in_values_listtotests/test_values.py. Both fail ondevelopwithOperationalError: no such columnand pass with the fix. The fulltests/test_values.pyandtests/test_queryset.pysuites pass, andruff/mypyare clean.